package middle.ground.utils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.ThreadLocalRandom;

/**
 * MD5工具类
 *
 * @author jrl
 */
public class Md5Util{

	/**
	 * 32位小写
	 *
	 * @param str
	 * @return
	 */
	private static final int MIN = 33;
	private static final int MAX = 126;
	private static final int SUM = 8;

	private static final char[] HEX_CHARS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

	public static String getMD5Str(String str) {
		MessageDigest messageDigest = null;
		try {
			messageDigest = MessageDigest.getInstance("MD5");
			messageDigest.reset();
			messageDigest.update(str.getBytes("UTF-8"));
		} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
			throw new RuntimeException(e);
		}

		byte[] byteArray = messageDigest.digest();
		StringBuffer md5StrBuff = new StringBuffer();
		for (int i = 0; i < byteArray.length; i++) {
			if (Integer.toHexString(0xFF & byteArray[i]).length() == 1) {
				md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
			} else {
				md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
			}
		}

		return md5StrBuff.toString();
	}

	/**
	 * 获取8位随机字符串
	 *
	 * @return
	 */
	public static String getStr() {
		ThreadLocalRandom r = ThreadLocalRandom.current();
		int num = 0;
		char result[] = new char[SUM];
		for (int i = 0; i < SUM; i++) {
			num = r.nextInt(MAX - MIN) + MIN;
			result[i] = (char) num;
		}
		return new String(result);
	}

	/**
	 * MD5编码
	 *
	 * @param origin 原始字符串
	 * @return 经过MD5加密之后的结果
	 */
	public static String md5Encode(String origin) {
		try {
			return ByteUtil.getHexString(MessageDigest.getInstance("MD5").digest(origin.getBytes()));
		} catch (NoSuchAlgorithmException e) {
			throw new RuntimeException(e);
		}
	}


	/**
	 * MD5加密
	 */
	public static String encrypt(byte[] value){
		try {
			byte[] bytes = MessageDigest.getInstance("MD5").digest(value);
			char[] chars = new char[32];
			for (int i = 0; i < chars.length; i = i + 2) {
				byte b = bytes[i / 2];
				chars[i] = HEX_CHARS[(b >>> 0x4) & 0xf];
				chars[i + 1] = HEX_CHARS[b & 0xf];
			}
			return new String(chars);
		} catch (Exception e) {
			throw new RuntimeException("md5 encrypt error", e);
		}
	}

	/**
	 * MD5加密
	 */
	public static String encrypt(String value){
		return encrypt(value.getBytes());
	}

	public static String digest(String str) {
		byte[] bytes = null;
		try {
			MessageDigest messageDigest = MessageDigest.getInstance("MD5");
			messageDigest.update(str.getBytes("UTF-8"));
			bytes = messageDigest.digest();
		} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
			throw new RuntimeException(e);
		}

		StringBuilder builder = new StringBuilder();
		for (int i = 0; i < bytes.length; i++) {
			if (Integer.toHexString(0xFF & bytes[i]).length() == 1) {
				builder.append("0").append(Integer.toHexString(0xFF & bytes[i]));
			} else {
				builder.append(Integer.toHexString(0xFF & bytes[i]));
			}
		}
		return builder.toString();
	}

}
